package graphmanip.collapsing;


import graphmanip.CollapsingMethod;

import java.awt.Point;
import java.util.ArrayList;


import commonfunctions.Functions;
import commonfunctions.Matrix;

public class NaiveHungarianCollapsing extends CollapsingMethod  
{
	public ArrayList<ArrayList<Integer>> collapse(Matrix weights) {
		int[] nodeTimeValues = Functions.getTimeMappings(weights);
		HungarianAlgo h = new HungarianAlgo(weights);
		int maxTimeValue = 0;
		for (int i = 0; i < nodeTimeValues.length; i++)
		{
			if (nodeTimeValues[i] > maxTimeValue)
				maxTimeValue = nodeTimeValues[i];
		}
		ArrayList<Point> pointsToAdd = new ArrayList<Point>();
		for (int i = 0; i < maxTimeValue; i++)
		{
			ArrayList<Integer> fromPoints = new ArrayList<Integer>();
			ArrayList<Integer> toPoints = new ArrayList<Integer>();
			for (int j = 0; j < nodeTimeValues.length; j++)
			{
				if (nodeTimeValues[j] == i)
					fromPoints.add(j);
				else if (nodeTimeValues[j] == i + 1)
					toPoints.add(j);
			}
			ArrayList<Point> nodesToDoAlgoOn = new ArrayList<Point>();
			for (int j = 0; j < fromPoints.size(); j++)
			{
				for (int k = 0; k < toPoints.size(); k++)
				{
					if (weights.get(fromPoints.get(j), toPoints.get(k)) > 0.0)
						nodesToDoAlgoOn.add(new Point(fromPoints.get(j), toPoints.get(k)));
				}
			}
			pointsToAdd.addAll(h.hungarianAlgorithm(nodesToDoAlgoOn));
		}
		ArrayList<ArrayList<Integer>> toReturn = new ArrayList<ArrayList<Integer>>();
		
		for (int i = 0; i < pointsToAdd.size(); i++)
		{
			ArrayList<Integer> toAdd = new ArrayList<Integer>();
			toAdd.add(pointsToAdd.get(i).x);
			toAdd.add(pointsToAdd.get(i).y);
			toReturn.add(toAdd);
		}
		return toReturn;
	}

}
